package io.bdmc.modules.bss.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.bdmc.common.annotation.SysLogger1;
import io.bdmc.common.utils.Constant.ModifyName;
import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.DeviceDevice;
import io.bdmc.modules.bss.entity.DeviceFrame;
import io.bdmc.modules.bss.service.IDeviceDeviceService;
import io.bdmc.modules.bss.service.IDeviceFrameService;
import io.bdmc.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 机架 前端控制器
 * </p>
 *
 * @author ping
 * @since 2020-06-29
 */

// "机架", description = "/bss/frame")
@RestController
@RequestMapping("/bss/frame")
public class DeviceFrameController extends AbstractController {

	@Autowired
	IDeviceFrameService _deviceframeSvs;

	@Autowired
	IDeviceDeviceService _deviceDeviceSvs;

	@GetMapping("/info")
	// "查询单个机架", notes = "查询单个机架")
	public R info(int frameId) {
		if (frameId <= 0) {
			return R.error(-1, "参数错误");
		}
		List<DeviceFrame> frame = _deviceframeSvs.getFramesAndDevicesByRoomId(0, frameId, getDeptId().intValue());
		if (frame.size() > 0) {
			return R.ok().putData(frame.get(0));
		} else {
			return R.ok().putData(frame);
		}
	}

	@GetMapping("/frames_devices")
	// "根据机房查询机架设备上所有的设备", notes = "根据机房查询机架设备上所有的设备")
	public R frameAndDevice(int equipmentroomId) {
		if (equipmentroomId <= 0) {
			return R.error(-1, "参数错误");
		}

		List<DeviceFrame> frames = _deviceframeSvs.getFramesAndDevicesByRoomId(equipmentroomId, 0,
				getDeptId().intValue());

		return R.ok().putData(frames);
	}

	@GetMapping("/devices")
	// "查询机架设备", notes = "查询机架设备")
	public R devices(int frameId) {
		if (frameId <= 0) {
			return R.error(-1, "参数错误");
		}
		QueryWrapper<DeviceDevice> w1 = new QueryWrapper<DeviceDevice>().eq("frame_id", frameId);
		return R.ok().putData(_deviceDeviceSvs.list(w1));
	}

	@SysLogger1(value = "新增机架", entityType = DeviceFrame.class, operationType = ModifyName.ADD)
	@PostMapping("/save")
	// "新增DeviceFrame", notes = "新增机架")
	@RequiresPermissions("bss:frame:save")
	public R save(@RequestBody DeviceFrame deviceframe) {
		deviceframe.setCreateBy(getUser().getUsername());
		// deviceframe.setCreateDate(LocalDateTime.now());
		deviceframe.setFrameId(null);
		deviceframe.setDeptId(getDeptId());
		Boolean b = deviceframe.insert();
		return R.trueOrFalse(b);
	}

	@SysLogger1(value = "修改机架", entityType = DeviceFrame.class, operationType = ModifyName.UPDATE)
	@PostMapping("/update")
	// "修改DeviceFrame", notes = "修改机架")
	@RequiresPermissions("bss:frame:update")
	public R update(@RequestBody DeviceFrame deviceframe) {
		Boolean b = deviceframe.updateById();
		return R.trueOrFalse(b);
	}

	@SysLogger1(value = "删除机架", entityType = DeviceFrame.class, operationType = ModifyName.DELETE)
	@PostMapping("/delete")
	// "删除DeviceFrame", notes = "删除机架")
	@RequiresPermissions("bss:frame:delete")
	public R delete(@RequestBody long frameId) {
		if (frameId > 0) {
			long devCount = _deviceDeviceSvs.count(new QueryWrapper<DeviceDevice>().eq("frame_id", frameId));
			if (devCount > 0) {
				return R.error(-1, "请先删除机架上的设备");
			}
			_deviceframeSvs.removeById(frameId);
		}

		return R.ok();
	}
}
